home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11458 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  92 lines

  1. Path: news.sover.net!news
  2. From: sstryker@sover.net (Stew Stryker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Newbie Question on using function ptr or Casting?
  5. Date: Thu, 14 Mar 1996 13:02:44 -0400
  6. Organization: Southern Vermont Network
  7. Message-ID: <19960314170244.sstryker@sover.net>
  8. NNTP-Posting-Host: pm0a1.wrj.sover.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-NewsReader: Emissary News v1.01.007, by Wollongong Inc.
  13.  
  14. [WARNING: C++ NEWBIE ALERT]
  15.  
  16. I'm working through some code a peer wrote, trying to use it for a new
  17. application.  In his code, he's used a technique that he calls casting that
  18. I'm not familiar with.
  19.  
  20. He has a base class which implements a linked list.  One of its methods is
  21. called get_next_record(), which returns the pointer to the next record
  22. (hence, the name...).
  23.  
  24. In the derived class, which holds a list of employees, he's printing out
  25. all the values in the list in a print() function (very good naming of
  26. methods, eh?  :^))
  27.  
  28. The print function looks like this:
  29.  
  30. void employees::print() {
  31.  
  32.     employees *current = this;
  33.  
  34.     while (current) {
  35.         cout "Employee name: " << current->emp_name << "\n";
  36.  
  37.             current = (employees*)current->get_next_record();
  38.         }
  39.  
  40.     cout << "End of employee list.\n";
  41. }
  42.  
  43. My confusion is on the line that reads:
  44.  
  45.         current = (employees*)current->get_next_record();
  46.  
  47. He says that he's casting the results of the linked_list get_next_record()
  48. function from a void pointer (which is how this base class method is
  49. defined) to a pointer of the employees type.  In my reading of my only C++
  50. manual (Teach Yourself C++ Programming in 21 Days, by Jesse Liberty), this
  51. looks kinda like a function pointer.  This book also refers briefly to
  52. casting of data, and says it's generally a sign of a poor design.
  53.  
  54. So, what is going on here?  Is there a better way to retrieve the pointer
  55. to the next record from the base function?  Can you try to put you answer
  56. in simple terms that a newbie could understand?
  57.  
  58. Thanks,
  59.  
  60. Stew
  61.  
  62. For those of you who really like this stuff (I'm not there yet), I've
  63. included the header for the linked list class:
  64.  
  65. class linked_list        {
  66.  
  67.   private:
  68.         void*           back_pointer;
  69.         linked_list      *next,
  70.                         *prev;
  71.  
  72.   public:
  73.         Boolean         last;
  74.         Boolean         first;
  75.  
  76.         int             status_code;
  77.  
  78.         linked_list(char*, void*);               // Constructor.
  79.         linked_list( linked_list*, char*, void*); // Constructor.
  80.         ~linked_list();                          // Destructor.
  81.  
  82.         virtual void            fetch_row();
  83.                 // Virtual function to print out the record.
  84.         virtual void            print();
  85.                 // True virtual to return the address of the derived class.
  86.                 // Function to access the next record.
  87.         void*   get_next_record();
  88.         void*   get_prev_record();
  89.         void    remove_from_list();
  90.         void    add_to_list(linked_list*);
  91. };
  92.